import 'dart:io' show Platform;
import 'package:casdon/app_providers.dart';
import 'package:casdon/core/auth/auth_storage.dart';
import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/modules/me/data/api/oss_sts_api.dart';
import 'package:casdon/modules/me/data/repository/me_repository.dart';
import 'package:casdon/modules/me/data/repository/me_repository_impl.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class OssStsUsecase {
  final MeRepository _repository;

  const OssStsUsecase(this._repository);

  Future<ResponseOssStsApi> execute() async {
    try {
      final userId = await getProvider(
        authStorageProvider.notifier,
      ).getUserId();
      if (userId == null) {
        throw Exception("OssStsUsecase 失败, userId 为空");
      }
      var ret = await _repository.getOSSStsToken(userId);
      if (ret == null) {
        throw Exception("OssStsUsecase 失败 结果为空");
      }
      return ret;
    } on BusinessException catch (e) {
      Log.e("OssStsUsecase 失败", e.toString(), StackTrace.current);
      rethrow;
    } catch (e) {
      Log.e("OssStsUsecase 失败", e.toString(), StackTrace.current);
      throw Exception("OssStsUsecase 失败");
    }
  }
}

final ossStsUsecaseProvider = Provider(
  (ref) => OssStsUsecase(ref.read(meRepositoryProvider)),
);
